/*
 * QuickSort.cpp
 *
 *  Created on: 13-09-2013
 *      Author: Krzychu
 */

#include "QuickSort.h"

QuickSort::QuickSort()
{

}

QuickSort::~QuickSort()
{

}

int QuickSort::partition(vector<int> data, int l, int r)
{
	int result;

	int x = data[l];
	int w;
	int i = l, j = r;
	while (true)
	{
		while (data[j] > x)
		{
			j--;
		}
		while (data[i] > x)
		{
			i++;
		}
		if (i < j)
		{
			w = data[i];
			data[i] = data[j];
			data[j] = w;
			i++;
			j--;
		}
		else
		{
			result = j;
			break;
		}
	}

	return result;
}

void QuickSort::sort(vector<int> data, int l, int r)
{
	int q;
	if (l < r)
	{
		q = partition(data, l, r);
		sort(data, l, q);
		sort(data, q + 1, r);
	}
}

void QuickSort::sort(vector<int> data)
{
	sort(data, 0, data.size() - 1);
}

